# -*- coding: utf-8 -*-
import io
from typing import Tuple
from PIL import Image
import numpy as np

try:
    import torch
    TORCH_OK = True
except Exception:
    TORCH_OK = False

def load_image_to_array(file_bytes: bytes, size: Tuple[int,int]=(224,224)):
    img = Image.open(io.BytesIO(file_bytes)).convert("RGB").resize(size)
    arr = np.asarray(img) / 255.0
    return arr

def to_model_input(arr: np.ndarray, as_torch: bool):
    if as_torch and TORCH_OK:
        import torch
        x = torch.from_numpy(arr.transpose(2,0,1)).unsqueeze(0).float()  # [1,3,H,W]
        return x
    else:
        # sklearn: 展平或直接返回 (这里返回展平示例)
        return arr.reshape(1, -1)
